/**
 * 
 */
package com.et114.components.mail;

import java.util.*;
import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.Assert;

/*
 * @author guanhw
 * SIMPALE
 */
public class MailAttachment {
    private static Log					logger				= LogFactory.getLog( MailAttachment.class );
    private Map map = new HashMap();
    private List list = new ArrayList( 1 ) ; 
    public MailAttachment() {
	// TODO Auto-generated constructor stub
    }

    public void addMailAttachment( String filename , String filePath  ) throws Exception {
    		Assert.state( !( filename == null || "".equals( filename.trim() ) ) );
    		Assert.state( !( filePath == null || "".equals( filePath.trim() ) ) );
    		File file = null;
    		try {
    			file  = new File( filePath ) ; 
    			if ( file.exists() && file.canRead()  ) {
    				map.put( filename.trim()  , file ) ;
    			}
    			else {
    				throw new Exception ( "file not find!" ) ; 
    			}	    
			}
			catch ( Exception e ) {
			    logger.error( e.getMessage() , e ) ; 
			}

    }
    
    public void addMailAttachment( String filePath  ) throws Exception {
		Assert.state( !( filePath == null || "".equals( filePath.trim() ) ) );
		File file = null;
		try {
			file  = new File( filePath ) ; 
			if ( file.exists() && file.canRead()  ) {
				list.add ( file ) ; 
			}
			else {
				throw new Exception ( "file not find!" ) ; 
			}	    
		}
		catch ( Exception e ) {
		    logger.error( e.getMessage() , e ) ; 
		}

}    
    
    public void addMailAttachment( String filename , File file  ) throws Exception { 
    		Assert.state( !( filename == null || "".equals( filename.trim() ) ));
    		Assert.state( !( file == null  ) );
    		map.put( filename , file ) ; 
    }
    
    
    public String[] listKeys () {
    		String[] keys = new String[ map.size() ]  ; 
    		return ( String[] )map.keySet().toArray( keys ) ; 
    }
    
    public File getMailAttachment( String filename ) {
    		return ( File  ) map.get( filename ) ;
    } 
    
    
    public List getAttachments ( ) {
		return list;
	}

	public void setAttachments ( List list ) {
		this.list = list;
	}

	/**
     * @param args
     */
    public static void main(String[] args) {
	// TODO Auto-generated method stub

    }

}
